<template>
  <el-container>
    <el-main>
      <el-row :gutter="20">
        <el-col :span="16">
          <div class="query-form">
            <span>查询语句</span>
            <el-input
              v-model="query"
              type="textarea"
              :rows="10"
              placeholder="请输入查询语句，例如 hget cart:bbhua"
              style="height: 500px; width: 60%;"
            />
          </div>
        </el-col>
        <el-col :span="8">
          <div class="options-form">
            <el-form label-width="100px">
              <el-form-item label="选择实例">
                <el-select v-model="selectedInstance" placeholder="请选择实例">
                  <el-option
                    v-for="item in instances"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>
              <el-form-item label="选择数据库">
                <el-select v-model="selectedDatabase" placeholder="请选择数据库">
                  <el-option
                    v-for="item in databases"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>
            </el-form>
            <div class="button-row">
              <el-button type="primary" @click="executeQuery">执行查询</el-button>
              <el-button @click="formatQuery">格式美化</el-button>
              <el-button @click="clearQuery">清空查询</el-button>
            </div>
          </div>
        </el-col>
      </el-row>
      <div>
    <!-- 表格搜索 -->
    <el-input
      placeholder="搜索"
      v-model="search"
      style="width: 200px; float: right; margin-bottom: 10px;"
    />

    <!-- 刷新按钮 -->
    <el-button
      icon="el-icon-refresh"
      circle
      style="float: right; margin-left: 10px; margin-bottom: 10px;"
      @click="refreshData"
    />

    <!-- 卡片形式展示（CSS样式需自定义） -->
    <el-button
      icon="el-icon-s-grid"
      circle
      style="float: right; margin-left: 10px; margin-bottom: 10px;"
      @click="toggleCardView"
    />

    <!-- 数据导出 -->
    <el-button
      icon="el-icon-download"
      circle
      style="float: right; margin-left: 10px; margin-bottom: 10px;"
      @click="exportData"
    />

    <!-- 动态列展示 -->
    <el-checkbox-group v-model="checkedColumns" style="float: right; margin-left: 10px; margin-bottom: 10px;">
      <el-checkbox v-for="column in columns" :label="column.prop" :key="column.prop">{{ column.label }}</el-checkbox>
    </el-checkbox-group>

    <el-table :data="queryHistory" style="width: 100%">
    <el-table-column
      v-for="column in columns"
      :key="column.prop"
      :prop="column.prop"
      :label="column.label"
    >
      <template #header="{ column, $index }">
        <div>
          {{ column.label }}
          <el-checkbox v-model="column.visible"></el-checkbox>
        </div>
      </template>
    </el-table-column>
  </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="queryHistory.length"
    />
  </div>
    </el-main>
  </el-container>
</template>

<script lang="ts">
import { defineComponent, ref ,computed } from 'vue';
import { ElMessage } from 'element-plus';

export default defineComponent({
  name: 'QueryPage',
  setup() {
    const query = ref('');
    const formatQuery = () => {
      // 这里实现格式美化的逻辑
      ElMessage.success('格式美化成功');
    };
    const clearQuery = () => {
      // 清空查询语句
      query.value = '';
    };

    const selectedInstance = ref('');
    const instances = ref([
      { value: 'instance1', label: '实例1' },
      { value: 'instance2', label: '实例2' },
    ]);
    const selectedDatabase = ref('');
    const databases = ref([
      { value: 0, label: '0' },
      { value: 1, label: '1' },
      { value: 2, label: '2' },
      { value: 3, label: '3' },
    ]);
    const queryHistory = ref([
      // 示例数据
      {
        instanceName: '实例1',
        databaseIndex: '0',
        queryTime: '2024-08-28 10:00:00',
        query: 'hget cart:bbhua',
        resultCount: 1,
        duration: 0.123,
      },
    ]);
    const search = ref('');
    const checkedColumns = ref(['instanceName', 'databaseIndex', 'queryTime', 'query', 'resultCount', 'duration']);
    const columns = ref([
      { prop: 'instanceName', label: '实例名称' ,visible: true},
      { prop: 'databaseIndex', label: '数据库index',visible: true },
      { prop: 'queryTime', label: '查询时间' ,visible: true},
      { prop: 'query', label: '查询语句' ,visible: true},
      { prop: 'resultCount', label: '查询返回条目数量' ,visible: true},
      { prop: 'duration', label: '查询耗时（秒）' ,visible: true},
    ]);
    const currentPage = ref(1);
    const pageSize = ref(10);
    // const queryHistory = ref([]); // 你需要根据实际情况来填充这个数组

    const filteredData = computed(() => {
      return queryHistory.value.filter(data => {
        return Object.keys(data).some(key => {
          return String(data[key]).toLowerCase().includes(search.value.toLowerCase());
        });
      }).slice((currentPage.value - 1) * pageSize.value, currentPage.value * pageSize.value);
    });

    const executeQuery = () => {
      // 这里应该是调用后端API执行查询的逻辑
      // 以下为模拟查询过程
      ElMessage.success('查询执行成功');
      // 假设返回了查询结果
      const result = {
        instanceName: selectedInstance.value,
        databaseIndex: selectedDatabase.value,
        queryTime: new Date().toLocaleString(),
        query: query.value,
        resultCount: 10, // 假设有10条数据
        duration: 0.234, // 假设耗时0.234秒
      };
      queryHistory.value.unshift(result); // 将新查询添加到历史记录的顶部
    };

    const handleSizeChange = (newSize: number) => {
      pageSize.value = newSize;
    };

    const handleCurrentChange = (newPage: number) => {
      currentPage.value = newPage;
    }; 
    const refreshData = () => {
      // 重新获取数据的逻辑
    };
    const toggleCardView = () => {
      // 切换卡片视图的逻辑，可能需要自定义CSS样式
    };


    return {
      formatQuery,
      clearQuery,
      query,
      selectedInstance,
      selectedDatabase,
      instances,
      databases,
      queryHistory,
      currentPage,
      pageSize,
      executeQuery,
      handleSizeChange,
      handleCurrentChange,
      search,
      checkedColumns,
      columns,
      // currentPage,
      // pageSize,
      // queryHistory,
      filteredData,
      refreshData,
      toggleCardView,
    };
  },
});
</script>

<style scoped>
.query-form {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.options-form {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.button-row {
  display: flex;
  /* justify-content: space-between; */
  margin-top: 20px;
}
</style>
